package com.silence.springboot.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * @author silence
 * @email 792245022@qq.com
 * @create 2023-09-20 11:30
 */
@Slf4j
public class EncryptionDecryptionUtil {

    /**
     * 利用Apache的工具类实现SHA-256加密
     *
     * @param str 加密后的报文
     * @return 加密后的字符串
     */
    public static String getSha256Str(String str) {
        String encodeStr = "";
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes(StandardCharsets.UTF_8));
            encodeStr = Hex.encodeHexString(hash);
        } catch (Exception e) {
            log.warn("SHA-256加密失败：", e);
        }
        return encodeStr;
    }

}
